Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
The defu npm package is a utility for performing default deep assignments on objects. It is commonly used to merge a set of objects into a single object, where the properties of later objects will overwrite those of earlier ones if they exist. This is particularly useful for managing configurations or options objects where default values need to be easily overridden by user inputs or other sources.
Deep Object Merging
This feature allows for the deep merging of objects. Properties from the second object (`objB`) are merged into the first object (`objA`), with nested properties being merged rather than replaced. This is useful for combining configurations with nested structures.
{"const defu = require('defu');\nconst objA = { foo: { bar: true } };\nconst objB = { foo: { baz: false }, qux: 1 };\nconst result = defu(objA, objB);\nconsole.log(result); // Output: { foo: { bar: true, baz: false }, qux: 1 }"}
Array Merging
This demonstrates how `defu` can be used to merge arrays contained within objects. Instead of replacing the array in `arrA` with the one in `arrB`, `defu` concatenates the arrays, preserving the order and contents of both.
{"const defu = require('defu');\nconst arrA = { arr: [1, 2] };\nconst arrB = { arr: [3, 4] };\nconst result = defu(arrA, arrB);\nconsole.log(result); // Output: { arr: [1, 2, 3, 4] }"}
Lodash's merge function offers similar deep merging capabilities as defu. However, lodash is a much larger library with a wide range of utilities beyond object merging. While defu is focused and lightweight, lodash provides a comprehensive suite of tools that may be beneficial for projects needing more than just object merging.
Deepmerge is another npm package that specializes in merging objects deeply. Like defu, it is focused on this specific task, but it offers more customization options, such as array merging strategies and cloning. This makes deepmerge a more flexible choice for complex merging needs, though it might be slightly more complex to use than defu.
Recursively assign default properties. Lightweight and Fast!
Install package:
npm install defu
OR
yarn add defu
const options = defu (object, ...defaults)
Most left arguments have more perioriry when assigning defaults.
const defu = require('defu')
console.log(defu({ 'a': { 'b': 2 } }, { 'a': { 'b': 1, 'c': 3 } }))
// => { a: { b: 2, c: 3 } }
object
and defaults
are not modifiednull
values are skipped same as defaults-deep. Please use either omit-deep or lodash.defaultsdeep if you need to to preserve.__proto__
and constructor
keys will be skipped to prevent security issues with object pollution.MIT. Made with 💖
FAQs
Recursively assign default properties. Lightweight and Fast!
The npm package defu receives a total of 3,407,252 weekly downloads. As such, defu popularity was classified as popular.
We found that defu demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.